home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7552 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Do you have ever pass structures?
  5. Date: 25 Feb 1996 12:58:59 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gqiijINN5ot@keats.ugrad.cs.ubc.ca>
  8. References: <4ge8mi$qjm@srvr1.engin.umich.edu> <4gnlf2$1fh@news.mistral.co.uk>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4gnlf2$1fh@news.mistral.co.uk>,
  12. Mike Barnard <mikebarnard@mistral.co.uk> wrote:
  13. >Hi.
  14. >
  15. >hasdi@news-server.engin.umich.edu (HASDI RODZMANN HASHIM) wrote:
  16. >
  17. >>MY QUESTION is do you ever pass the whole structures to a function
  18. >>or you just pass a pointer to a function? For example, for a struct
  19. >>of the following size...
  20. >
  21. >I'm new at this and not very good. But I have been reading on this
  22. >subject ant it seems that if you pass the whole structure it takes a
  23. >lot longer at the whole structure is copied onto the stack. If this is
  24. >in a loop it could take up valuable code time. Also if you are for
  25. >some reason close to the end of the stack you could overrun it and
  26. >have a stack overflow error.
  27.  
  28. This is largely irrelevant. Pushing a small word onto the stack can blow it
  29. just as well. Your point is valid in that passing big structures will add a lot
  30. of bloat to the stack depth of a recursive functions, perhaps needlessly so.
  31. On the other hand if a private copy of the data is really needed, then you are
  32. really arguing against the design of the algorithm which calls for that
  33. requirement.
  34.  
  35. Modern architectures/operating systems have large stacks, by the way, that are
  36. limited basically by available virtual memory rather than the possibility of a
  37. stack/heap collision.
  38.  
  39. >If you pass a pointer only the value of the pointer is put on the
  40. >stack. Saves much time and stack space.
  41.  
  42. Buy you may need a local copy of the structure. Suppose I had:
  43.  
  44. typedef struct complex {
  45.     double real;
  46.     double imaginary;
  47. } complex_t;
  48.  
  49. complex_t my_recursive_fun(complex_t C)
  50.  
  51. {
  52.     if (/* condition */) {
  53.         /* code which perhaps does something to C */
  54.         return my_recursive_fun(C);
  55.     else {
  56.         /* some other code */
  57.         return C;
  58.     }
  59. }
  60.  
  61. If you passed and returned the structure by pointer, you not be able to use
  62. such recursion. You would have to declare an complex_t's on the stack, and
  63. explicitly make local copy, use the local copy recursively, and then copy the
  64. result over top of the original to pass the value back.
  65.  
  66. This method is syntactically cleaner. A good compiler might optimize it by
  67. allowing the return complex_t to occupy the same spot as the parameter,
  68. depending on what is possible in the environment, thus making it probably no
  69. more wasteful of memory or cycles than the version which uses explicit copying.
  70. -- 
  71.  
  72.